﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BaiDuSitemap.Setting
{
    public partial class URL : Form
    {
        #region Attributes

        public ucURL myURL = null;
        public bool IsAdd = true;

        private string url = "";
        private List<string> urls = null;
        private string separator = ";";

        #endregion

        public URL()
        {
            InitializeComponent();
        }

        private void URL_Load(object sender, EventArgs e)
        {
            InitComboBox();

            if (!IsAdd)
            {
                this.Text = "网址信息修改";
                InitForm();
            }
            else
            {
                this.Text = "添加网址";
            }
        }

        #region Event

        #region Button

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.cbbGroup.SelectedIndex == 0)
            {
                MessageBox.Show("请选择分组");
                return;
            }

            if (IsAdd && !Add()) return;
            if (!IsAdd && !Modify()) return;            

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #endregion

        #region Method

        private void InitForm()
        {
            DataGridView dgv = myURL.dgvURL;
            urls = new List<string>();

            foreach (DataGridViewRow row in dgv.SelectedRows)
            {
                urls.Add(row.Cells[0].Value.ToString());
            }

            this.tbURL.Text = string.Join(separator, urls.ToArray());
            if (urls.Count > 1) this.tbURL.ReadOnly = true;

            this.cbbGroup.SelectedIndex = this.cbbGroup.Items.Count > myURL.cbbGroup.SelectedIndex ? myURL.cbbGroup.SelectedIndex : 0;
        }

        private void InitComboBox()
        {
            DataTable dt = DBO.Group.getAll();
            DataRow dr = dt.NewRow();
            dr[0] = "0";
            dr[1] = "-请选择-";
            dr[2] = "";
            dt.Rows.InsertAt(dr, 0);

            this.cbbGroup.ValueMember = "gid";
            this.cbbGroup.DisplayMember = "gname";
            this.cbbGroup.DataSource = dt;
            this.cbbGroup.SelectedValue = myURL.cbbGroup.SelectedValue;
        }

        private bool Add()
        {
            int result = DBO.URL.Add(this.tbURL.Text, int.Parse(this.cbbGroup.SelectedValue.ToString()));
            switch (result)
            {
                case -2:
                    MessageBox.Show("网址已存在");
                    return false;
                case 1:
                    MessageBox.Show("添加成功！");
                    break;
                default:
                    MessageBox.Show("操作失败，错误代码：" + result);
                    return false;
            }
            myURL.lblRefresh_Click(new object(), new EventArgs());
            return true;
        }

        private bool Modify()
        {
            int result = this.tbURL.ReadOnly ? DBO.URL.Update4Main(urls, int.Parse(this.cbbGroup.SelectedValue.ToString())) : DBO.URL.Update4Main(url, this.tbURL.Text, int.Parse(this.cbbGroup.SelectedValue.ToString()));
            switch (result)
            {
                case -2:
                    MessageBox.Show("网址不存在");
                    return false;
                case -1:
                case 0:
                    MessageBox.Show("操作失败， 错误代码：" + result);
                    return false;
                default:
                    MessageBox.Show(string.Format("修改完成，共修改{0}条记录", result));
                    break;
            }

            return true;
        }

        #endregion
    }
}
